package com.lkm.market2_0.m_imple.steelyardserver;


import com.lkm.hardware_connect.IHardwareControler;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;




class ScoketHardwareControler implements IHardwareControler {

    private Socket socket;

    public ScoketHardwareControler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public boolean isConnected() {
        return socket.isConnected();
    }

    @Override
    public int write(byte[] data) {
        try {
            OutputStream outputStream=socket.getOutputStream();
            outputStream.write(data);
            outputStream.flush();
            return data.length;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int read(byte[] buf, int len) {
        if(!socket.isConnected())return -1;
        try {

            return socket.getInputStream().read(buf,0,len);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void close() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
